#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "options.h"
#include "light_beat.h"

void print_help(int argc, char *argv[])
{
        if (argc >= 1) {
                fprintf(stderr, "Usage: %s -v -n -h -l <LOGFILE> -p <PIDFILE> -f <PULSEDEVICE> -r <SAMPLERATE> -b <BUFFERSIZE>\n", argv[0]);
                fprintf(stderr, "Beat a light to the music playing on a PulseAudio device.\n\n");

                fprintf(stderr, "  -v\t\t\tVerbose mode\n");
                fprintf(stderr, "  -n\t\t\tTurn off daemon mode (Nondetached)\n");
                fprintf(stderr, "  -h\t\t\tPrint this help screen\n");
                fprintf(stderr, "  -l <LOGFILE>\t\tWrite the stderr to this file\n");
                fprintf(stderr, "  -p <PIDFILE>\t\tUse this pid file\n");
                fprintf(stderr, "  -f <PULSEDEVICE>\tTap off of this device.\n");
                fprintf(stderr, "  -r <SAMPLERATE>\tDefault: 44100\n");
                fprintf(stderr, "  -r <BUFFERSIZE>\tDefault: 2048\n\n");
                fprintf(stderr, "To get the pulseaudio device, try running:\n");
                fprintf(stderr, " pactl list | grep monitor\n\n");
        }
        exit(1);
}

lightoptions * get_options(int argc, char *argv[])
{

        lightoptions * opt = (lightoptions *)malloc(sizeof(lightoptions));
        int c;

        memset(opt, 0, sizeof(lightoptions));

        opt->daemonmode = 1;
        opt->pidfile = PIDFILE;
        opt->bufsize = BUFSIZE;
        opt->rate = RATE;
        opt->pulsedev = DEVICE;
        opt->logfile = "/dev/null";

        while ((c = getopt(argc, argv, "vnl:p:hf:r:b:")) != -1) {
                switch (c) {
                case 'v':
                        opt->debug = 1;
                        break;
                case 'n':
                        opt->daemonmode = 0;
                        break;
                case 'l':
                        opt->logfile = optarg;
                        break;
                case 'p':
                        opt->pidfile = optarg;
                        break;
                case 'h':
                        opt->help = 1;
                        print_help(argc, argv);
                        break;
                case 'f':
                        opt->pulsedev = optarg;
                        break;
                case 'r':
                        opt->rate = atoi(optarg);
                        break;
                case 'b':
                        opt->bufsize = atoi(optarg);
                        break;
                }
        }
        return opt;
}





